<?php

/**
 * Class de base qui gere les exceptions levees par le framework
 *
 * @category   McCube
 * @package    Bundle
 * @author     Leiha Sellier <leiha.sellier@gmail.com>
 * @license    http://creativecommons.org/licenses/by/3.0/  CC BY 3.0
 * @link       http://code.google.com/p/mechanics-cubes/
 */
class McCube_Exception
    extends    Exception
    implements McCube_Exception_Interface{

    public $type      = '';
    public $statusMsg = 'Error';

    public function __construct($type, $errorCode, $extras=array()){

        parent::__construct($type, $errorCode);
    }

    public function _getLitteralError($errorCode){
        switch($error){
            case 404 : $txt = 'Not Found';break;
        }
        return $txt;
    }


    /** */
    public static function handler($exception){
        if(McCube_Router::getInstanceOf()->ajaxRequest)
            header('SERVERERRORS:'. self::_renderAjax($exception));
        else
            echo self::_renderHTML($exception);
    }

    /** */
    public function render($exception){
        return $exception->renderHTML();
    }

    /** */
    public function renderHTML($exception){
        if(method_exists($exception, 'renderHTML'))
            $message = $exception->renderHTML();
        else
            $message = $exception->getMessage();

        return '<div style="font-size:13px;margin:10px;padding:10px;background-color:beige;border:1px solid red;">'
            .'<div style="font-weight:bold;color:red;margin-bottom:5px;">'.$exception->statusMsg.' !</div>'
            .'<span style="font-weight:bold;color:red;">'.$message.'</span>'
            .'<br />&rArr; <strong>BackTrace :</strong> '
            .'[ <em style=";color:green;">'.self::_getLastFunction($exception).'</em> ] '
            .$exception->getFile().':'.$exception->getline()
            .'</div>'
        ;
    }

    protected function _getLastFunction($exception){
        $traces = $exception->getTrace();

        $function = $traces[0]['function'];
        if(!empty($traces[0]['class']))
            $function = $traces[0]['class'].$traces[0]['type'].$traces[0]['function'];

        return $function;
    }

}